package cn.bouncyslime.graduationProject.nba.controller;

import cn.bouncyslime.graduationProject.nba.dto.PerennialPlayer;
import cn.bouncyslime.graduationProject.nba.dto.TeamDto;
import cn.bouncyslime.graduationProject.nba.service.PlayerDataService;
import cn.bouncyslime.graduationProject.nba.service.TeamService;
import cn.bouncyslime.graduationProject.nba.util.ResponseData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 作者： 张一卓
 * 时间： 2020/3/30 0:23
 */
@Controller
public class TeamController {

    private static final Logger logger = LoggerFactory.getLogger(TeamController.class);

    @Autowired
    TeamService teamService;

    @RequestMapping("listAllTeams")
    public List<TeamDto> listAllTeams(){
        return teamService.listAllTeams();
    }

    @RequestMapping(value = "toTeamListPage", method = {RequestMethod.POST, RequestMethod.GET})
    public String toTeamListPage(ModelMap modelMap){
        List<TeamDto> historyTeams = teamService.listHistoryTeams();
        List<TeamDto> existingTeams = teamService.listExistingTeams();
        modelMap.addAttribute("historyTeams", historyTeams);
        modelMap.addAttribute("existingTeams", existingTeams);
        return "teamInfo";
    }

    @RequestMapping(value = "toTeamPage", method = {RequestMethod.POST, RequestMethod.GET})
    public String toTeamPage(ModelMap modelMap, @RequestParam String teamId){
        TeamDto team = teamService.getTeam(teamId);
        List<String> allSeasonOfTeam = teamService.listAllSeasonOfTeam(teamId);
        Map teamRadarMap = teamService.getTeamRadarMap(teamId);
        modelMap.addAttribute("team", team);
        modelMap.addAttribute("seasons", allSeasonOfTeam);
        modelMap.addAttribute("teamRadarMap", teamRadarMap);
        return "team";
    }

    @ResponseBody
    @RequestMapping(value = "getTeamRadarMap", method = {RequestMethod.POST})
    public ResponseData getTeamRadarMap(@RequestBody Map<String, String> map){
        String teamId = map.get("teamId");
        ResponseData responseData = ResponseData.success(teamService.getTeamRadarMap(teamId));
        return responseData;
    }

    @RequestMapping(value = "loadPerennialPlayersPage", method = {RequestMethod.POST, RequestMethod.GET})
    public String loadPerennialPlayersPage(ModelMap modelMap, @RequestParam String teamId, @RequestParam String season){
        List<PerennialPlayer> perennialPlayers = teamService.listRSPerennialPlayers(teamId, season);
        modelMap.addAttribute("seasonType", "rs");
        modelMap.addAttribute("perennialPlayers", perennialPlayers);
        modelMap.addAttribute("currentSeason", season);
        modelMap.addAttribute("currentTeam", teamId);
        return "rsPerennialPlayer";
    }

    @RequestMapping(value = "listRSPerennialPlayer", method = {RequestMethod.POST, RequestMethod.GET})
    public String listRSPerennialPlayer(ModelMap modelMap, @RequestParam String teamId, @RequestParam String season){
        List<PerennialPlayer> perennialPlayers = teamService.listRSPerennialPlayers(teamId, season);
        modelMap.addAttribute("seasonType", "rs");
        modelMap.addAttribute("perennialPlayers", perennialPlayers);
        modelMap.addAttribute("currentSeason", season);
        modelMap.addAttribute("currentTeam", teamId);
        return "rsPerennialPlayer";
    }

    @RequestMapping(value = "listPSPerennialPlayer", method = {RequestMethod.POST, RequestMethod.GET})
    public String listPSPerennialPlayer(ModelMap modelMap, @RequestParam String teamId, @RequestParam String season){
        List<PerennialPlayer> perennialPlayers = teamService.listPSPerennialPlayers(teamId, season);
//        logger.debug(perennialPlayers.toString());
        modelMap.addAttribute("seasonType", "ps");
        modelMap.addAttribute("perennialPlayers", perennialPlayers);
        modelMap.addAttribute("currentSeason", season);
        modelMap.addAttribute("currentTeam", teamId);
        return "psPerennialPlayer";
    }
}